<?php
require_once SF_ROOT_DIR.'../../../../symfony_lib/MPDF45/mpdf.php';

class FoxsisMPDF extends mPDF
{
	protected $filename;
	protected $img_dir;
	protected $txt_cabecalho_aux;
	/*
	 * margens do corpo do PDF. Devem ser setadas em mm.
	 */
	protected $margin_l;
	protected $margin_r;
	protected $margin_t;
	protected $margin_b;
	protected $margin_h;
	protected $margin_f;

	protected $tp_data_header;
	protected $my_tp_header;



	public function __construct($filename)
	{

		parent::__construct();

		$this->filename 			= strtolower($filename).".pdf";

		$this->img_dir				= sfConfig::get('sf_web_dir').'\\images\\';
		$this->useOnlyCoreFonts 	= true;//liberando acesso � fonte times, setada no mpdf.css
		$this->setAutoTopMargin		= 'stretch';//margin do header em relacao ao texto

		/*
		 * Definindo padr�es ao sistema. Para os que n�o seguirem as mesmas normas, basta alterar na classe especifica
		 */
		$this->txt_cabecalho_aux	= '';
		$this->margin_l				= 15;
		$this->margin_r				= 15;
		$this->margin_t				= 15;
		$this->margin_b				= 15;
		$this->margin_h				= 15;
		$this->margin_f				= 15;
		//$this->SetBasePath('file://www.meusite.com/');

		//$this->showImageErrors 		= true;
		$this->my_tp_header			= array();
		$this->tp_data_header		= array("Ministerio" 	=> array("Ministerio" 	=> 'SBS Q.2 Bloco F Edif�cio FNDE - 70.070-929 - Bras�lia, DF<br/>
																			Telefone: (61) 2022-4153 Fax (61) 2022-4108 - E-mail: divap@fnde.gov.br'),
											"FNDE" 			=> array("FNDE"			=> "Auditoria Interna<br/>
																			Divis�o de Auditoria de Programas"),
											"Advocacia" 	=> array("Advocacia"	=> "SBS Qd. 02 - Bloco F - Ed. �urea - 3� Andar - Sl. 301 - CEP 70070-929 - Bras�lia/DF <br />
																						Telefone: (61) 212-4804/4858 - Fax: (61) 212.4876 - e-mail: evanderson@fnde.gov.br")
											);

	}

	protected function rodape($texto = null)
	{
		$rodape = '<table style="font-size:10px;"><tr>';
		$rodape .= '<td width="570px;" style="text-align:center;">'.utf8_encode($texto).'</td>';
		$rodape .= '<td> {PAGENO} de {nb} </td>';
		$rodape .= '</tr></table>';

		$this->SetHTMLFooter($rodape);

	}

	protected function getHeader($tp_header = null, $dynamic = null)
	{
		if($tp_header)
		{
			if(count($this->my_tp_header))
			{
				foreach($this->my_tp_header as $k => $v)
					$this->tp_data_header[$k] = $v;
			}

			$key			= array("FNDE");

			if(array_key_exists($tp_header, $this->tp_data_header))
			{
				$key = array_keys($this->tp_data_header[$tp_header]);

			}

			$method_header	= "cabecalho".$key[0];

			$this->$method_header($this->tp_data_header[$tp_header][$key[0]]);
		}
		else
		{
			$this->SetHTMLHeader($dynamic);
		}
	}

	protected function cabecalhoSecundario($texto = null, $hasNumberPage = false)
	{
		$cabecalho = '<table style="font-size:10px;"><tr>';
		$cabecalho .= '<td>'.utf8_encode($texto).'</td>';

		if($hasNumberPage)
			$cabecalho .= '<td> {PAGENO} de {nb} </td>';

		$cabecalho .= '</tr></table>';

		$this->SetHTMLHeader($cabecalho);
	}


	protected function cabecalhoFNDE($texto = null)
	{
		$cabecalho = '<table align="left">
						<tr>
							<td valign="middle">
								'.image_tag("logo\logo_relatorio.jpg").'
							</td>
							<td align="left">
								<b>MINIST�RIO DA EDUCA��O</b><br/>
								Fundo Nacional de Desenvolvimento da Educa��o<br/>
								'.$texto.'
							</td>
						</tr>
					</table>';

		$this->SetHTMLHeader(utf8_encode($cabecalho));
	}

	protected function cabecalhoAdvocacia($texto = null)
	{
		$cabecalho = '<div align="center">
						'.image_tag("logo.jpg", array("width"=>80)).'
						<br/><br/>
						<b>	ADVOCACIA-GERAL DA UNI�O<br />
							PROCURADORIA-GERAL FEDERAL<br>
							PROCURADORIA FEDERAL - FNDE</b><br/>
						<span style="">'.$texto.'</span>
					</div>';

		$this->SetHTMLHeader(utf8_encode($cabecalho));
	}

	protected function cabecalhoMinisterio($texto = null)
	{
		$cabecalho = '<div align="center"  style="font-size:12px;">
						'.image_tag("logo.jpg", array("width"=>80)).'
						<br/><br/>
						<b>MINIST�RIO DA EDUCA��O</b><br/>
						FUNDO NACIONAL DE DESENVOLVIMENTO DA EDUCA��O - FNDE<br/>
						<span style="font-size:11px;">'.$texto.'</span>
					</div>';

		$this->SetHTMLHeader(utf8_encode($cabecalho));
	}

	public function gerarPDF($content, $tp_header = null, $rodape = null, $dynamicHeader = null, $output = 'D', $orientation = 'P')
	{
		$this->getHeader($tp_header, $dynamicHeader);

		$this->rodape($rodape);

		$this->AddPage($orientation, '', '', '', '', $this->margin_l, $this->margin_r, $this->margin_t, $this->margin_b, $this->margin_h, $this->margin_f); //margin-left, margin-right, margin-top, margin-bottom, margin-header; EM milimetros
		$stylesheet = file_get_contents(_MPDF_PATH.'mpdf.css');
		$this->WriteHTML($stylesheet,1);
		$this->cabecalhoSecundario($this->txt_cabecalho_aux);

		$this->WriteHTML(utf8_encode($content));
    	$this->Output($this->filename, $output);
	}
}

?>